<?php

namespace app\api\controller;

use app\api\Validate\IntentionValidate;
use app\common\controller\Api;
use think\Config;
use think\Exception;

/**
 * 求职意向表
 */
class Intention extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    protected $failException = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\api\model\Intention();
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 列表
     * @return void
     */
    public function list()
    {
        try {
            $data = $this->model->alias('m')
                ->join('personage_post p','m.personage_post_id=p.id')
                ->order('m.id desc')
                ->where('m.user_id',$this->auth->getUserinfo()['id'])
                ->field('m.*,p.name as post_name')
                ->select();
            $this->success('ok', $data);
        }catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 添加
     * @return void
     */
    public function add()
    {
        $input = input('post.');
        try {
            $input['user_id'] =  $this->auth->getUserinfo()['id'];
            $this->validate($input,IntentionValidate::class.'.add');
            $this->model->insert($input);
            $this->success('ok');
        }catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 修改
     * @return void
     */
    public function save()
    {
        $input = input('post.');
        try {
            $this->validate($input,IntentionValidate::class.'.save');
            $id = $input['id'];
            unset($input['id']);
            $this->model->where('id',$id)->update($input);
            $this->success('ok',['id' => $id]);
        }catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }



}